package com.zsck.pos.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.chinaums.commoncheck.R;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.zsck.pos.adapter.ProjectAdapter;
import com.zsck.pos.base.BaseTitleActivity;
import com.zsck.pos.bean.Project;
import com.zsck.pos.net.MyObserver;
import com.zsck.pos.net.NetConfig;
import com.zsck.pos.net.RetrofitCilent;
import com.zsck.pos.utils.LogUtil;
import com.zsck.pos.utils.SharePreferenceUtils;
import com.zsck.pos.view.PopManager;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zsck.net.Utils.RxHelper.observableIO2Main;

public class MultiBillsConditionActivity extends BaseTitleActivity implements OnDateSetListener {
    //    @BindView(R.id.sp_project)
//    Spinner spinnerProject;
    //    @BindView(R.id.sp_pay_role)
//    Spinner spinnerRole;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.btn_search)
    Button btnSearch;

    @BindView(R.id.tv_start_month)
    TextView tvStartMonth;
    @BindView(R.id.tv_end_month)
    TextView tvEndMonth;
    @BindView(R.id.tv_project)
    TextView tvProject;
    @BindView(R.id.tv_customer)
    TextView tvCustomer;
    @BindView(R.id.tv_contract)
    TextView tvContract;
    @BindView(R.id.et_contract)
    EditText etContract;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.tv_pay_role)
    TextView tvPayRole;
    @BindView(R.id.iv_1)
    ImageView iv1;
    @BindView(R.id.iv_2)
    ImageView iv2;
    @BindView(R.id.iv_3)
    ImageView iv3;
    @BindView(R.id.iv_4)
    ImageView iv4;
    @BindView(R.id.pay_type)
    TextView payType;
    @BindView(R.id.parent_cons)
    ConstraintLayout parentCons;
    @BindView(R.id.ll_pay_role)
    LinearLayout llPayRole;
    @BindView(R.id.tv_project_cur)
    TextView tvProjectCur;
    @BindView(R.id.ll_project)
    LinearLayout llProject;


    private boolean ISSTART = false;
    private boolean ISEND = false;
    private String collectionObjId;//缴费类型2企业,1个人

    TimePickerDialog mDialogYearMonth;
    private Project mProject;
    private List<Project> projectList = new ArrayList<>();


    @Override
    public int setLayout() {
        return R.layout.activity_bills_condition_layout;
    }

    @Override
    public String setTitle() {
        return getString(R.string.bills_pay);
    }

    @Override
    public Activity setActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        String curDate = simpleDateFormat.format(date);
        tvStartMonth.setText(curDate);
        tvEndMonth.setText(curDate);
        mDialogYearMonth = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH)
                .setTitleStringId("选择时间")
                .setToolBarTextColor(getResources().getColor(R.color.white))
                .setThemeColor(getResources().getColor(R.color.bottom_text_checked))
                .setCallBack(this)
                .build();
        setProjectSpiiner();
    }

    private void showProjectPop() {
        PopManager.getInstance().showBottomProject(MultiBillsConditionActivity.this, projectList, new PopManager.OnItemClick() {
            @Override
            public void onItemClick(int position) {
                mProject = projectList.get(position);
                tvProjectCur.setText(mProject.getName());
            }

            @Override
            public void setCancelIitemClick() {

            }
        });
    }

    private void showRolePop() {
        String[] stringArray = getResources().getStringArray(R.array.pay_roles);
        PopManager.getInstance().showBottomPopWindow(MultiBillsConditionActivity.this, Arrays.asList(stringArray), new PopManager.OnItemClick() {
            @Override
            public void onItemClick(int position) {

                switch (position) {
                    case 0:
                        collectionObjId = "2";
                        break;
                    case 1:
                        collectionObjId = "1";
                        break;
                    case 2:
                        collectionObjId = "";
                        break;
                    default:
                        collectionObjId = "";
                        break;
                }
                payType.setText(stringArray[position]);
            }

            @Override
            public void setCancelIitemClick() {

            }
        });

    }

    @OnClick({R.id.ll_project, R.id.ll_pay_role, R.id.btn_search, R.id.tv_start_month, R.id.tv_end_month})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_project:
                showProjectPop();
                break;
            case R.id.ll_pay_role:
                showRolePop();
                break;
            case R.id.tv_start_month:
                ISSTART = true;
                ISEND = false;
                mDialogYearMonth.show(getSupportFragmentManager(), "start");
                break;
            case R.id.tv_end_month:
                ISSTART = false;
                ISEND = true;
                mDialogYearMonth.show(getSupportFragmentManager(), "end");
                break;
            case R.id.btn_search:
                String contractNo = etContract.getText().toString();
                String condition = etSearch.getText().toString();
                String beginMonth = tvStartMonth.getText().toString();
                String endMonth = tvEndMonth.getText().toString();
                if(!TextUtils.isEmpty(contractNo)&&contractNo.length()<4){
                    Toast.makeText(this,"合同编号不能少于4位数",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intentMu = new Intent();
                ComponentName componentNameMu = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.MultiBillsActivity");
                intentMu.setComponent(componentNameMu);
                intentMu.putExtra("project", (Serializable) mProject);
                intentMu.putExtra("condition", condition);
                intentMu.putExtra("contractNo", contractNo);
                intentMu.putExtra("collectionObjId", collectionObjId);
                intentMu.putExtra("beginMonth", beginMonth);
                intentMu.putExtra("endMonth", endMonth);
                startActivity(intentMu);
                break;

        }
    }


    private void setProjectSpiiner() {
        if (NetConfig.projects == null || NetConfig.projects.size() == 0) {
            getProjectAppSelectList();
        } else {
            projectList.addAll(NetConfig.projects);
            if (projectList.size() > 0) {
                mProject = projectList.get(0);
                tvProjectCur.setText(projectList.get(0).getName());
            }
        }
    }

    /*查询项目列表*/
    private void getProjectAppSelectList() {
        projectList.clear();
        String possn = SharePreferenceUtils.getString(this, "POSSN");
        Map<String, Object> map = new HashMap<>();
        if (!TextUtils.isEmpty(possn)) {
            map.put("posId", possn);
        }
        MyObserver<List<Project>> observer = new MyObserver<List<Project>>(this) {
            @Override
            public void onSuccess(List<Project> result) {
                if (result != null && result.size() > 0) {
                    NetConfig.projects = result;
                    mProject = result.get(0);
                    projectList.addAll(result);
//                    projectAdapter.notifyDataSetChanged();
//                    spinnerProject.setSelection(0);
                } else {
                    Toast.makeText(MultiBillsConditionActivity.this, "没有项目权限", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LogUtil.e("test", e.getMessage());
            }

            @Override
            public void reRequest() {
                getProjectAppSelectList();
            }
        };
//        RetrofitCilent.getApiService().getProjectAppSelectList(map).
//                compose(observableIO2Main(this)).subscribe(observer);
        RetrofitCilent.getApiService().queryProject(map).
                compose(observableIO2Main(this)).subscribe(observer);
    }

    /*日期选择*/
    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String text = getDateToString(millseconds);
        if (ISSTART) {
            tvStartMonth.setText(text);
        } else {
            tvEndMonth.setText(text);
        }

    }

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");

    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }

}
